package com.maszy.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.maszy.bean.Print;
import com.maszy.service.PrintService;

public class PrintServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	PrintService service = new PrintService();
	String view = new String(); // 页面路径

	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		
		PrintServlet servlet = new PrintServlet();
		RequestDispatcher rd = null;
		String method = request.getParameter("method");
	if ("queryAll".equals(method)) {
			// 查询出全部数据
			servlet.queryAll(request, response);
			view = "/background/print/print.jsp";
		} else if ("update".equals(method)) {
			// 新增/修改记录
			
			servlet.update(request, response);
			servlet.queryAll(request, response);
			view = "/background/print/print.jsp";
		} else if ("delete".equals(method)) {
			// 删除记录
			servlet.delete(request, response);
			servlet.queryAll(request, response);
		} else if ("updateForward".equals(method)) {
			// 进入修改页面
			servlet.queryForOne(request, response);
			view = "/background/print/update_print.jsp";
		} else if ("insertForward".equals(method)) {
			// 进入添加页面
			view = "/background/print/update_print.jsp";
		} 

		try {
			rd = this.getServletContext().getRequestDispatcher(view);
			rd.forward(request, response);

		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String id=request.getParameter("print_id");
		if(!"".equals(id)&&id!=null){
			Print obj=new Print();
			obj.setPrint_id(Integer.parseInt(id));
			service.delete(obj);
		}
	}

	private void queryForOne(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		String id=request.getParameter("print_id");
		if(!"".equals(id)&&id!=null){
			Print obj=new Print();
			obj.setPrint_id(Integer.parseInt(id));
			obj=service.queryById(obj);
			request.setAttribute("obj", obj);
		}
	}

	private void update(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		Print obj=new Print();
		String id=request.getParameter("print_id");		
		if(!"".equals(id)&&id!=null){		
			//修改数据
			obj.setPrint_id(Integer.parseInt(id));
			obj.setPrint_name(request.getParameter("print_name"));
			obj.setPrint_ip(request.getParameter("print_ip"));
			obj.setRemark(request.getParameter("remark"));
			service.update(obj);
		}else{
			//新增数据
			obj.setPrint_name(request.getParameter("print_name"));
			obj.setPrint_ip(request.getParameter("print_ip"));
			obj.setRemark(request.getParameter("remark"));
			service.add(obj);	
		}
	}

	private void queryAll(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		List list = service.queryAll();
		request.setAttribute("list", list);
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		doGet(request, response);
	}
}